<?php
namespace Illuminate\Auth;

use InvalidArgumentException;

trait CreatesUserProviders {
    protected $customProviderCreators = [];

    public function createUserProvider($provider = null) {
        if ( is_null($config = $this->getProviderConfiguration($provider)) ) return; // 获取配置

        if ( isset($this->customProviderCreators[$driver = ($config['driver'] ?? null)]) ) {
            return call_user_func($this->customProviderCreators[$driver], $this->app, $config);
        }

        switch ( $driver ) {
            case 'database':
                return $this->createDatabaseProvider($config);
            case 'eloquent':
                return $this->createEloquentProvider($config);
            default:
                throw new InvalidArgumentException(
                    "Authentication user provider [{$driver}] is not defined."
                );
        }
    }
    protected function createDatabaseProvider($config) {
        $connection = $this->app['db']->connection(); // 数据库连接

        return new DatabaseUserProvider($connection, $this->app['hash'], $config['table']);
    }
    protected function createEloquentProvider($config) {
        return new EloquentUserProvider($this->app['hash'], $config['model']);
    }

    protected function getProviderConfiguration($provider) { // 提供器配置
        if ( $provider = $provider ?: $this->getDefaultUserProvider() ) return $this->app['config']['auth.providers.'.$provider];
    }
    public function getDefaultUserProvider() { // 默认提供器
        return $this->app['config']['auth.defaults.provider'];
    }
}
